package com.parking.status.sync.netty.util;

import java.io.*;

/**
 * Created by Administrator on 2015/9/15.
 */
public class ByteObjConverter {
    public static Object ByteToObject(byte[] bytes) {
        Object obj = null;
        ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
        ObjectInputStream oi = null;
        try {
            oi = new ObjectInputStream(bi);
            obj = oi.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bi.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                oi.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return obj;
    }

    public static byte[] ObjectToByte(Object obj) {
        byte[] bytes = null;
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream oo = null;
        try {
            oo = new ObjectOutputStream(bo);
            oo.writeObject(obj);
            bytes = bo.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                oo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return (bytes);
    }

//    public static byte[] Int32ToBytesBigEndian(int int32)
//    {
//        byte[] ret = new byte[4];
//        ret[3]=(byte)(int32>>24);
//        ret[2]=(byte)(int32>>16);
//        ret[1]=(byte)(int32>>8);
//        ret[0]=(byte)(int32);
//        return ret;
//    }
//
//    public static int BytesBigEndianToInt32(byte[] buf) {
//
//        return (buf[3]<<24) + (buf[2]<<16) + (buf[1]<<8) + buf[0];
//    }
}
